import axios from 'axios'
import config from './config'
import router from '@/router'
import { ElMessage } from 'element-plus'

export default function request(options) {
    return new Promise((resolve, reject) => {
        const service = axios.create({...config })
            // 请求拦截器
        service.interceptors.request.use(
                (config) => {
                    // 发送请求的时候携带Token
                    let token = localStorage.getItem('token')
                        // 判断是否携带token
                    if (token) {
                        config.headers.token = token
                    } else {
                        router.push('/login')
                    }
                    return config
                },
                //请求发生错误时
                (error) => {
                    console.log('request:', error)
                    if (
                        error.code === 'ECONNABORTED' &&
                        error.message.indexOf('timeout') !== -1
                    ) {
                        // showClose为true则是用户可以自己关闭
                        ElMessage({ message: '请求超时', type: 'error', showClose: true })
                    }
                    return Promise.reject(error)
                }
            ),
            // 响应拦截器
            service.interceptors.response.use(
                (response) => {
                    return response.data
                },
                (err) => {
                    if (err && err.response) {
                        switch (err.response.status) {
                            case 400:
                                err.message = '请求错误'
                                break
                            case 401:
                                err.message = '未授权，请登录'
                                break
                            case 403:
                                err.message = '拒绝访问'
                                break
                            case 400:
                                err.message = '请求错误'
                                break
                            case 404:
                                err.message = '请求地址出错：${err.response.config.url}'
                                break
                            case 408:
                                err.message = '请求超时'
                                break
                            case 500:
                                err.message = '服务器内部错误'
                                break
                            case 501:
                                err.message = '服务未实现'
                                break
                            case 502:
                                err.message = '网关错误'
                                break
                            case 503:
                                err.message = '服务不可用'
                                break
                            case 504:
                                err.message = '网关超时'
                                break
                            case 505:
                                err.message = 'HTTP版本不受支持'
                                break
                            default:
                        }
                    }
                    console.error(err)
                    if (err.message) {
                        ElMessage({ message: err.message, type: 'error', showClose: true })
                    }
                    return Promise.reject(err)
                }
            ),
            // 处理正确返回结果的数据
            service(options)
            .then((res) => {
                if (res.code === 200 || res.code === -5) {
                    resolve(res)
                } else if (res.code === -1) {
                    ElMessage({
                        message: '用户名被注册，请更换',
                        type: 'error',
                        showClose: true,
                    })
                    reject(res)
                } else if (res.code === -2) {
                    ElMessage({
                        message: '用户名或者密码不正确',
                        type: 'error',
                        showClose: true,
                    })
                    reject(res)
                } else if (res.code === -3) {
                    ElMessage({
                        message: res.message, // 俩次输入的密码不正确！ 后端重新验证
                        type: 'error',
                        showClose: true,
                    })
                    reject(res)
                } else if (res.code === -4) {
                    ElMessage({
                        message: res.message || '无权限、需要重新登录',
                        type: 'error',
                        showClose: true,
                    })
                    reject(res)
                }
            })
            .catch((error) => {
                reject(error)
            })
    })
}